﻿function Ensure-Ending([string] $path, [string] $ending){
	if($path.EndsWith($ending)){
		return $path
	} else {
		return $path + $ending
	}
}

function Ensure-Wildcard([string] $path){
	if($path.EndsWith("\*")){
		return $path
	} else {
		if($path.EndsWith("\")){
			return $path + '*'
		} else {
			return $path + "\*"
		}
	}
}

function encode([string] $xml) 
      { 
         $tempContent = new-object System.IO.StringWriter 
         $textwriter = new-object System.Xml.XmlTextWriter $tempContent 
         $textWriter.WriteString($xml) 
         $tempContent.ToString() 
      }
. .\tidy-xml.ps1


function Svn-Add($wcfile) { 
	# svn add "D:\Scripting\SvnTest\Project1\*" --force
	
	write-host Reading file $wcfile ...
	$wc = Get-Content $wcfile
	
	foreach ($path in $wc) {
		write-host WorkingCopy-Path: $path
	}
  
    write-host Running svn add...
	foreach ($path in $wc) {
		$path = Ensure-Wildcard $path
		write-host Running command: svn add $path --force
		svn add $path --force
	}

}



function Svn-Info([string] $path) {
	switch -regex (svn st $path) {
		"^?" {$other+=1}
		"^A" {$added+=1}
		"^M" {$modified+=1}
		default {}
	}
	write-host "SVN o:$other a:$added m:$modified >"
}


$xml1=[xml] @'
<?xml version="1.0"?>
<status>
	<target
		path="D:\Scripting\SvnTest\Project1">
		<entry
			path="D:\Scripting\SvnTest\Project1\1">
			<wc-status
			   props="none"
			   item="unversioned">
			</wc-status>
		</entry>
		<entry
			path="D:\Scripting\SvnTest\Project1\1.txt">
			<wc-status
			   props="none"
			   item="unversioned">
			</wc-status>
		</entry>
	</target>
</status>
'@




$xml2= @'
<?xml version="1.0"?>
<info>
	<entry
	   kind="dir"
	   path="D:\Scripting\SvnTest\Project1"
	   revision="1">
			<url>file:///D:/Scripting/SvnTest/Repository/Project1</url>
			<repository>
				<root>file:///D:/Scripting/SvnTest/Repository</root>
				<uuid>ca1a5bb5-27b2-0241-afed-c2fd906a6ba4</uuid>
			</repository>
			<wc-info>
			<schedule>normal</schedule>
			<depth>infinity</depth>
			</wc-info>
			<commit
			   revision="1">
				<author>Anton</author>
				<date>2008-12-26T13:24:11.389139Z</date>
			</commit>
	</entry>
</info>



<?xml version="1.0"?>
<status>
  <target path="D:\Scripting\SvnTest\Project1">
    <entry path="D:\Scripting\SvnTest\Project1">
      <wc-status props="none" item="normal" revision="1">
        <commit revision="1">
          <author>Anton</author>
          <date>2008-12-26T13:24:11.389139Z</date>
        </commit>
      </wc-status>
    </entry>
    <entry path="D:\Scripting\SvnTest\Project1\1">
      <wc-status props="none" item="normal" revision="5">
        <commit revision="5">
          <author>Anton</author>
          <date>2008-12-29T15:14:36.453741Z</date>
        </commit>
      </wc-status>
    </entry>
    <entry path="D:\Scripting\SvnTest\Project1\1\xxxxxxxx2.txt">
      <wc-status props="none" item="unversioned">
      </wc-status>
    </entry>
    <entry path="D:\Scripting\SvnTest\Project1\1\2.txt">
      <wc-status props="none" item="missing" revision="6">
        <commit revision="6">
          <author>Anton</author>
          <date>2008-12-29T15:15:22.113741Z</date>
        </commit>
      </wc-status>
    </entry>
    <entry path="D:\Scripting\SvnTest\Project1\1\1.txt">
      <wc-status props="none" item="normal" revision="5">
        <commit revision="5">
          <author>Anton</author>
          <date>2008-12-29T15:14:36.453741Z</date>
        </commit>
      </wc-status>
    </entry>
  </target>
</status>


# define Consts for all the numeric status values
NONE = 1
UNVERSIONED = 2
NORMAL = 3
ADDED = 4
MISSING = 5
DELETED = 6
REPLACED = 7
MODIFIED = 8
MERGED = 9
CONFLICTED = 10
IGNORED = 11
OBSTRUCTED = 12
EXTERNAL = 13
INCOMPLETE = 14
# I added these so I can amalgamate repo and local status
UPDATED = 15
MODIFIED_NEWER = 16
NEWFILE = 17


'@
	  
function Svn-Sync([string] $path) {
	$items = 0
	$xml=[xml](svn status $path --xml -v)
	foreach ($target in $xml.status.target){
		foreach ($entry in $target.entry){
			if($target.path -eq $entry.path){
				write-host "WorkingCopy: " $entry.path
			} else {
				$state = $entry."wc-status".item
				switch ($state) {
					"unversioned" {
						svn add $entry.path
						$items++
					}
					"modified"{
						$items++
					}
					"added" {
						$items++
					}
					"missing"{
						svn delete $entry.path
						$items++
					}
					"deleted" {
						svn delete $entry.path
						$items++
					}
				}
			}
		}
		if($items -gt 0) {
			write-host Commiting  $items items...
			svn commit $path -m "auto"
		} else {
			write-host Nothing to commit!
		}
	}
	#tidy-xml $xml
}

function Get-SvnRevision($dir) { 
	##info.entry.url
	##info.entry.revision 
	([xml](svn info $dir --xml)).info.entry.url 
}


$tempPath = [System.IO.Path]::GetTempPath()
$tempDirName = [System.IO.Path]::GetRandomFileName()
$workingCopy = Join-Path $tempPath $tempDirName

function Get-LocalPath($uri) { 
	##info.entry.url
	##info.entry.revision 
	
	($uri=[system.URI] $uri).LocalPath
	#$uri.LocalPath
	
}




function xprompt {
	$host.ui.rawui.WindowTitle = $(get-location)
	if(Test-Path _svn) {
		switch -regex (svn st) {
			"^?" {$other+=1}
			"^A" {$added+=1}
			"^M" {$modified+=1}
			default {}
		}
		$prompt_string = "SVN o:$other a:$added m:$modified >"
	} else {
		$prompt_string = "PS >"
	}

	Write-Host ($prompt_string) -nonewline -foregroundcolor yellow
	
	return "xxxxxxxxxxxxxxx "
}